import { createRouter , createWebHistory } from "vue-router"
import { userStore } from "../store/index"
const routes = [
    {
        path:"/",
        component: ()=>import ("../view/home/index.vue"),
        redirect: "/home", 
        children: [
            {
                path:"/home",
                name: "Home",
                component: ()=>import ("../view/home/content/index.vue")
            },
            {
                path:"/chat",
                name:"Chat",
                component: ()=>import ("../view/home/content/chat.vue")
            },
            {
                path:"/worker",
                name:"Worker",
                component: ()=>import ("../view/home/content/worker.vue"),
                children: [
                    {
                        path: "/talk",
                        name: "talk",
                        component: ()=>import("../view/home/content/worker/talk.vue")
                    },
                    {
                        path: "/media",
                        name: "Media",
                        component: ()=>import("../view/home/content/worker/media.vue")
                    },
                    {
                        path: "/meeting",
                        name: "Meeting",
                        component: ()=>import("../view/home/content/worker/meeting.vue")
                    },
                    {
                        path: "/map",
                        name: "Map",
                        component: ()=>import("../view/home/content/worker/map.vue")
                    }
                ]
            }
        ]

    },
    {
        path:"/login",
        name: "Login",
        component: ()=>import ("../view/login/index.vue")
    }
]



const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach(async (to,_,next)=>{
    const userInfo = userStore()
    console.log(to,"前置路由守卫中-------------",userInfo.$state.userInfo.token);
    if(sessionStorage.getItem("cxl_Token")){
        await userInfo.saveUserInfo({username:'',password:''})
    }
    if (to.name !== 'Login' && !userInfo.$state.userInfo.token) {
        console.log(to,"返回login",userInfo);
        next({ name: 'Login' })
    }else {
        console.log(to,"跳转",userInfo);
        next()
    }
}) 

export default router